package com.qingxin.sys.verifier;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证器规则
 * @author Administrator
 *
 */
public abstract class VerifyRule {

	protected String ruleArgs;
	protected boolean foreground;
	protected boolean background;
	protected String error;
	
	private VerifyItem verifyItem;
	
	
	public VerifyRule() {
		this.foreground = true;
		this.background = true;
	}
	
	
	/**
	 * 后台验证
	 * 
	 * @param id 字段ID
	 * @param name 字段名
	 * @param label 字段说明
	 * @param formMap
	 * @return 验证不通过时的错误信息，如果验证通过返回null
	 */
	public abstract VerifyMsg isValid(String id, String name, String label, FormMap formMap);
	
	
	/**
	 * 获取前台验证配置
	 * 
	 * @param name
	 * @param label
	 * @return
	 */
	public abstract Map<String, Object> getRuleCfg(String name, String label);
	
	
	/**
	 * 创建验证错误信息
	 * 
	 * @param paramMap
	 * @param args 一组数量为偶数的数组，奇数为参数名，偶数为参数值
	 * @return
	 */
	public String createMessage(Map<String, String> paramMap, String... args){
		if(getError() != null){
			if(paramMap != null && args != null){
				for(int i=0, n=args.length/2; i<n; i++){
					paramMap.put(args[i*2], args[i*2+1]);
				}
			}
			Matcher m = Pattern.compile("\\{(.+?)\\}").matcher(getError());
			StringBuffer buf = new StringBuffer();
			while(m.find()){
				if(paramMap == null){
					paramMap = new HashMap<String, String>();
					if(args != null){
						for(int i=0, n=args.length/2; i<n; i++){
							paramMap.put(args[i*2], args[i*2+1]);
						}
					}
				}
				String tag = m.group(0);
				if(paramMap.containsKey(tag)){
					String tagValue = paramMap.get(tag);
					m.appendReplacement(buf, Matcher.quoteReplacement(tagValue==null ? "" : tagValue));
				}
			}
			m.appendTail(buf);
			return buf.toString();
		}
		return "";
	}
	
	
	public String getRuleArgs() {
		return ruleArgs;
	}
	
	/**
	 * 设置规则表达式的参数，包括作用域以及其他专用参数，默认处理作用域参数，如果包括其他参数则需要子类重写该方法并进行处理<br>
	 * 参数默认一个字符表示一个参数，区分大小写，可以使用下划线定义长参数名如[_empty_]且将长参数置于单字符参数之后<br>
	 * 默认对作用域的处理为禁用指定作用域，即参数包含[f]时禁用前台验证、包含[b]时禁用后台验证<br>
	 * 参数是区分大小写的
	 * 
	 * @param ruleArgs 表达式参数列表，对其解析后处理
	 */
	public void setRuleArgs(String ruleArgs){
		this.ruleArgs = ruleArgs;
		if(ruleArgs != null){
			ruleArgs = ruleArgs.replace("_", "__");
			Matcher m = Pattern.compile("[0-9A-Za-z]|_[0-9A-Za-z]+_").matcher(ruleArgs);
			while(m.find()){
				handleRuleArg(m.group());
			}
		}
	}
	
	
	/**
	 * 对解析到的单个表达式参数
	 * 
	 * @param arg
	 */
	protected void handleRuleArg(String arg){
		if("f".equals(arg)){
			setForeground(false);
		}else if("b".equals(arg)){
			setBackground(false);
		}
	}
	
	
	public boolean isForeground() {
		return foreground;
	}
	public void setForeground(boolean foreground) {
		this.foreground = foreground;
	}
	public boolean isBackground() {
		return background;
	}
	public void setBackground(boolean background) {
		this.background = background;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	
	
	public VerifyItem getVerifyItem() {
		return verifyItem;
	}
	public void setVerifyItem(VerifyItem verifyItem) {
		this.verifyItem = verifyItem;
	}


	protected boolean isEmpty(String string){
		return string == null || string.trim().length() == 0;
	}
	
}
